package com.nmu.app.Shrimp.veiw.javagame.dto;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;

import com.nmu.app.Shrimp.veiw.javagame.IVisableObject;
import com.nmu.app.Shrimp.veiw.javagame.util.KeyboardInput;
import com.nmu.app.Shrimp.veiw.javagame.util.MouseInput;

//基础2D平行运动物体
public class Albt implements IVisableObject{
	private ContextFor2D context;//环境因子
	private Point lastPosition=new Point();		//上一位置
	private Point currentPosition=new Point();	//当前位置
	private double velocity=0;			//速度
	private String shape;			//模型
	private double footLength=1;
	//临时
	private int width;
	private int hight;
	private volatile BufferStrategy bufferStrategy;//绘图操作对象
	
	public Albt(int positionX, int positionY, int Wide, int Hight,ContextFor2D ctx) {
		this.context=ctx;
		this.width=Wide;this.hight=Hight;this.currentPosition.x=positionX;this.currentPosition.y=positionY;
	}

	@Override
	public void render(Graphics g) {
			int w=width;int h=hight;int x=currentPosition.x;int y=currentPosition.y;
			g.setColor(Color.BLUE);
			g.drawRect(x, y, width, hight);
//			g.draw
	}

	@Override
	public void update(Point p) {
		//重力因素
//		s=vot+1/2at^2
		//于水平面之上
		if(context.staticY>currentPosition.y+hight|| velocity<0){
			currentPosition.y=(int) (currentPosition.y+(velocity*footLength+(context.getGravity()*footLength*footLength)/2));
			double v0=velocity;
			double vt=0.7*velocity+context.getGravity()*footLength;
			velocity=vt;
			//单位运动量
			double moveS=(0.3)*((vt*vt-v0*v0)/(2*context.getGravity()));
			currentPosition.y=(int) (currentPosition.y+moveS);
			
			
			if(currentPosition.y+hight>context.staticY)
				currentPosition.y=context.staticY-hight;
		}else{
			currentPosition.y=context.staticY-hight;
			velocity=velocity>0?0:velocity;
		}
		
	}

	@Override
	public void inputProcess(KeyboardInput k) {
		KeyboardInput keyboard=new KeyboardInput();
		keyboard=k;
		keyboard.poll();
		if(keyboard.keyDown(KeyEvent.VK_LEFT)){
			currentPosition.x=currentPosition.x>2?currentPosition.x-2:currentPosition.x/2;
			System.out.println("X-2:"+currentPosition.x);
		}
		if(keyboard.keyDown(KeyEvent.VK_RIGHT)){
			currentPosition.x+=2;
			System.out.println("X+2:"+currentPosition.x);
		}
		if(keyboard.keyDownOnce(KeyEvent.VK_C)){
			currentPosition.y=currentPosition.x=0;
		}
		if(keyboard.keyDownOnce(KeyEvent.VK_SPACE)){
			System.out.println("ijump");
			velocity=-30;
		}
		
	}


	@Override
	public void mouseProcess(MouseInput mi) {
		// TODO Auto-generated method stub
		
	}
}
